﻿Imports DTO
Imports BUS
Public Class ucBoPhan

    Dim BoPhanDTO As New BoPhanDTO

    Private Sub LayDanhSachBoPhan()
        GridControl1.DataSource = BoPhanBUS.LayDanhSachBoPhan()
    End Sub

    Private Sub ucBoPhan_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        barbtnSuaChuaBP.Enabled = False
        barbtnXoaBP.Enabled = False
        barbtnXuatBP.Enabled = False
        LayDanhSachBoPhan()
    End Sub

    Private Sub barbtnThemBP_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnThemBP.ItemClick
        Dim frm As New frmThemQuanLy
        AddHandler frm.LamMoiNVKhiThem, AddressOf LayDanhSachBoPhan
        frm.ShowDialog()
    End Sub

    Private Sub barbtnXoaBP_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnXoaBP.ItemClick
        Dim pq As PhanQuyenNhonNDDTO = ChucNangBUS.layQuyenCuaND(frmMain.maND, 27)
        If pq.Xoa = True Then
            Try
                If BoPhanBUS.XoaBoPhan(BoPhanDTO) Then
                    DevExpress.XtraEditors.XtraMessageBox.Show("Đã xóa thành công", "Thành công", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    LayDanhSachBoPhan()
                End If
            Catch ex As Exception
                DevExpress.XtraEditors.XtraMessageBox.Show("Không thể xóa bộ phận này vì đang được sử dụng", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
                BoPhanBUS.DongKetNoi.Close()
            End Try
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Xin lỗi bạn không có quyền này", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        
    End Sub

    Private Sub barbtnNapLaiBP_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnNapLaiBP.ItemClick
        LayDanhSachBoPhan()
    End Sub

    Private Sub GridView1_RowClick(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowClickEventArgs) Handles GridView1.RowClick
        If e.RowHandle >= 0 Then
            barbtnSuaChuaBP.Enabled = True
            barbtnXoaBP.Enabled = True
            BoPhanDTO.MaBoPhan = GridView1.GetRowCellValue(e.RowHandle, gvcMaBP).ToString()
            BoPhanDTO.TenBoPhan = GridView1.GetRowCellValue(e.RowHandle, gvcTenBP).ToString()
            BoPhanDTO.GhiChu = GridView1.GetRowCellValue(e.RowHandle, gvcGhiChu).ToString()
            BoPhanDTO.MaTrangThai = Convert.ToInt32(GridView1.GetRowCellValue(e.RowHandle, gvcConQuanLy))
        End If
    End Sub

    Private Sub barbtnSuaChuaBP_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnSuaChuaBP.ItemClick
        Dim frm As New frmThemQuanLy
        frm.maBP = BoPhanDTO.MaBoPhan
        frm.TenBP = BoPhanDTO.TenBoPhan
        frm.GhiChu = BoPhanDTO.GhiChu
        frm.ConQL = BoPhanDTO.MaTrangThai
        frm.Text = "Sửa chữa Bộ Phận"
        frm.kt = 1
        frm.ShowDialog()
    End Sub

    Private Sub GridView1_CustomDrawRowIndicator(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs) Handles GridView1.CustomDrawRowIndicator
        If e.RowHandle >= 0 Then
            If e.Info.IsRowIndicator Then
                e.Info.DisplayText = (e.RowHandle + 1).ToString()
            End If
        End If
    End Sub
End Class
